using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using Ares.CommonObjects;
using Ares.Protocols;

namespace cb0tServer
{
    partial class ServerCore
    {
        private bool OnJoinFilterBefore(UserObject userobj)
        {
            FilterItem[] items = JoinFilters.GetFilters();
            String ip = userobj.physical_ip.ToString();
            String lan_ip = userobj.local_ip.ToString();

            if (items == null) return true;

            foreach (FilterItem filter in items)
            {
                switch (filter.type)
                {
                    case "censor":
                        if (userobj.name.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1)
                        {
                            String str = userobj.name;
                            str = Helpers.RemoveChars(str, filter.trigger);
                            str = NameHelpers.FormatChangingName(str);
                            if (str == null) break;
                            if (UserPool.NameExists(userobj, str)) break;
                            userobj.orgName = str;
                            userobj.name = str;
                        }

                        break;

                    case "ipban":
                        if (ip.StartsWith(filter.trigger) || lan_ip.StartsWith(filter.trigger)) return false;
                        break;

                    case "nameban":
                        if (userobj.name.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1) return false;
                        break;

                    case "portban":
                        ushort portban_port;
                        
                        if (ushort.TryParse(filter.trigger, out portban_port))
                        {
                            if (userobj.dc_port == portban_port) return false;
                        }

                        break;

                    case "versionban":
                        if (userobj.version.ToUpper().StartsWith(filter.trigger.ToUpper())) return false;
                        break;

                    case "changename":
                        if (ip.StartsWith(filter.trigger) ||
                            userobj.name.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1)
                        {
                            String str2 = NameHelpers.FormatChangingName(filter.args);
                            if (str2 == null) break;
                            if (UserPool.NameExists(userobj, str2)) break;
                            userobj.name = str2;
                        }

                        break;

                    case "move":
                        if (ip.StartsWith(filter.trigger) ||
                            userobj.name.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1)
                        {
                            ushort jfilter_move;

                            if (ushort.TryParse(filter.args, out jfilter_move))
                            {
                                userobj.vroom = jfilter_move;
                            }
                        }

                        break;

                    case "redirect":
                        if (ip.StartsWith(filter.trigger) ||
                            userobj.name.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1)
                        {
                            ChannelListItem clistitem = Hashlinks.DecodeHashlink(filter.args);

                            if (clistitem != null)
                            {
                                userobj.auto_redirect = true;
                                userobj.SendOutboundPacket(ServerOutboundPackets.RedirectPacket(clistitem));
                                return false;
                            }
                        }

                        break;

                    case "reservename":
                        if (userobj.name == filter.args)
                            if (ip != filter.trigger)
                                return false;

                        break;
                }
            }

            return true;
        }

        private void OnJoinFilterAfter(UserObject userobj)
        {
            FilterItem[] items = JoinFilters.GetFilters();
            String ip = userobj.physical_ip.ToString();

            if (items == null) return;

            foreach (FilterItem filter in items)
            {
                switch (filter.type)
                {
                    case "antiflood":
                        if (ip == filter.trigger) userobj.antiflood = true;
                        break;

                    case "echo":
                        if (ip.StartsWith(filter.trigger) ||
                            userobj.name.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1)
                        {
                            userobj.echo = true;
                            userobj.echo_text = filter.args.Replace("+n", userobj.name).Replace("+ip", userobj.physical_ip.ToString());
                        }

                        break;

                    case "paint":
                        if (ip.StartsWith(filter.trigger) ||
                            userobj.name.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1)
                        {
                            userobj.painted = true;
                            userobj.painted_text = filter.args.Replace("+n", userobj.name).Replace("+ip", userobj.physical_ip.ToString());
                        }

                        break;

                    case "clone":
                        if (ip.StartsWith(filter.trigger) ||
                            userobj.name.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1)
                        {
                            UserPool.Broadcast(userobj.vroom, ServerOutboundPackets.PublicTextPacket(userobj.name,
                                filter.args.Replace("+n", userobj.name).Replace("+ip", userobj.physical_ip.ToString())));
                        }

                        break;

                    case "changemessage":
                        if (ip.StartsWith(filter.trigger) ||
                            userobj.name.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1)
                        {
                            if (filter.args.Length > 0)
                            {
                                PMessages.Add(userobj, filter.args);
                            }
                        }

                        break;

                    case "announce":
                        if (ip.StartsWith(filter.trigger) ||
                            userobj.name.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1)
                        {
                            UserPool.Broadcast(userobj.vroom, ServerOutboundPackets.AnnoucePacket(filter.args.Replace("+n",
                                userobj.name).Replace("+ip", userobj.physical_ip.ToString())));
                        }

                        break;

                    case "pm":
                        if (ip.StartsWith(filter.trigger) ||
                            userobj.name.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1)
                        {
                            userobj.SendOutboundPacket(ServerOutboundPackets.PMPacket(CoreSettings.room_bot,
                                filter.args.Replace("+n", userobj.name).Replace("+ip", userobj.physical_ip.ToString())));
                        }

                        break;

                    case "ipsend":
                        if (ip.StartsWith(filter.trigger) ||
                            userobj.name.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1)
                        {
                            if (userobj.level >= CommandLevels.GetLevel(CommandLevels.LevelCodes.ipsend))
                            {
                                userobj.ipsend = true;
                                this.SendUserIPSend(userobj);
                            }
                        }

                        break;

                    case "logsend":
                        if (ip.StartsWith(filter.trigger) ||
                            userobj.name.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1)
                        {
                            if (userobj.level >= CommandLevels.GetLevel(CommandLevels.LevelCodes.logsend))
                            {
                                userobj.logsend = true;
                            }
                        }

                        break;

                    case "bansend":
                        if (ip.StartsWith(filter.trigger) ||
                            userobj.name.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1)
                        {
                            if (userobj.level >= CommandLevels.GetLevel(CommandLevels.LevelCodes.bansend))
                            {
                                userobj.bansend = true;
                            }
                        }

                        break;

                    case "vspy":
                        if (ip.StartsWith(filter.trigger) ||
                            userobj.name.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1)
                        {
                            if (userobj.level >= CommandLevels.GetLevel(CommandLevels.LevelCodes.vspy))
                            {
                                if (userobj.admin) userobj.vspy = true;
                            }
                        }

                        break;

                    case "disableavatar":
                        if (ip.StartsWith(filter.trigger) ||
                            userobj.name.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1)
                        {
                            userobj.avatar_count = 10;
                        }

                        break;
                }
            }
        }

        private void SendUserIPSend(UserObject userobj)
        {
            foreach (UserObject u in UserPool.Users)
            {
                if (u.IsSet())
                {
                    if (u.logged_in)
                    {
                        userobj.SendOutboundPacket(ServerOutboundPackets.PMPacket(CoreSettings.room_bot,
                            "IPSEND: " + u.name + " " + u.physical_ip.ToString() + " " +
                            u.local_ip.ToString() + " " + u.dc_port));
                    }
                }
            }
        }
    }
}
